//
//  DAUserListTableViewCell.m
//  DAKnowledge
//
//  Created by DreamArts on 16/6/3.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAUserListTableViewCell.h"
#import "DALabel.h"
#import "Masonry.h"
#import "Utility.h"
#import "DAHeader.h"
#import "DAUserModel.h"
#import "UIImageView+WebCache.h"

@interface DAUserListTableViewCell ()
@property (nonatomic, strong) UIImageView *userImageView;
@property (nonatomic, strong) DALabel *userNameLabel;
@property (nonatomic, strong) DALabel *userSignLabel;
@property (nonatomic, strong) DALabel *fileNumLabel;
@end

@implementation DAUserListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
       self.contentView.backgroundColor = [UIColor whiteColor];
        // 头像
        self.userImageView = [[UIImageView alloc]init];
        self.userImageView.layer.cornerRadius = 5;
        self.userImageView.clipsToBounds = YES;
        [self.contentView addSubview:self.userImageView];
        
        //头像添加点击方法
        UITapGestureRecognizer *userImageTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickUserImage:)];
        self.userImageView.userInteractionEnabled = YES;
        [self.userImageView addGestureRecognizer:userImageTap];
        
        // 用户名
        self.userNameLabel = [[DALabel alloc]init];
        self.userNameLabel.textColor = [UIColor blackColor];
        [self.contentView addSubview:self.userNameLabel];
        
        // 个性签名
        self.userSignLabel = [[DALabel alloc]init];
        self.userSignLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        self.userSignLabel.font = [UIFont systemFontOfSize:12];
        self.userSignLabel.textColor = [UIColor lightGrayColor];
        self.userSignLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        [self.userSignLabel textRectForBounds:CGRectMake(self.userSignLabel.frame.origin.x, self.userSignLabel.frame.origin.y, self.userSignLabel.frame.size.width, self.userSignLabel.frame.size.height) limitedToNumberOfLines:0];
        self.userSignLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        [self.contentView addSubview:self.userSignLabel];
        
        //文件个数
        self.fileNumLabel = [[DALabel alloc]init];
        self.fileNumLabel.textColor = [UIColor lightGrayColor];
        self.fileNumLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.fileNumLabel];
        
    }
    return self;
}

- (void)clickUserImage:(UITapGestureRecognizer *)tap{
    
    [self.delegate userDetailWithUserId:self.userModel.userID];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    // 头像
    [self.userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(20);
        make.width.height.mas_equalTo(40);
    }];
    // 用户名
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(self.userImageView.mas_right).offset(10);
        make.bottom.mas_equalTo(self.userSignLabel.mas_top).offset(-5);
        make.right.mas_equalTo(-20);
    }];
    
    // 个性签名
    [self.userSignLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.userNameLabel.mas_bottom).offset(5);
        make.left.mas_equalTo(self.userNameLabel);
        make.right.mas_equalTo(self.fileNumLabel.mas_right).offset(-60);
        make.height.mas_equalTo(20);
    }];
    
    // 文件个数
    [self.fileNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.userNameLabel.mas_bottom).offset(7);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-15);
    }];
    
}

- (void)setUserModel:(DAUserModel *)userModel{
    if (_userModel != userModel) {
        _userModel = userModel;
    }
   // [self.userImageView sd_setImageWithURL:[NSURL URLWithString:userModel.userImage]];
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:userModel.userImage] placeholderImage:[UIImage imageNamed:@"holderface"] options:SDWebImageRefreshCached];
    
    [self.userNameLabel setText:userModel.userName fitToContent:YES];
    [self.userSignLabel setText:userModel.userSign fitToContent:YES];
    NSString * file_num = NSLocalizedString(@"files", nil);
    [self.fileNumLabel setText:[NSString stringWithFormat:@"%@ %@",userModel.fileNum,file_num] fitToContent:YES];
    if (self.str != nil) {
        NSMutableAttributedString *newString = [[NSMutableAttributedString alloc]initWithString:self.str];
        NSRange range = [userModel.userName  rangeOfString:self.str];
        [newString addAttribute:NSForegroundColorAttributeName value:[UIColor supportRedColor] range:range];
        self.userNameLabel.attributedText = newString;
    }

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
